package com.baizhi.controller;


import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("guru")
public class GuruController {
    @Autowired
    private GuruService guruService;

    @RequestMapping("selectAll")
    @ResponseBody
    public Map selectAll(String nickName,Integer page,Integer limit){
        Map m = new HashMap();
        PageInfo<Guru> info = guruService.selectAll(nickName, page, limit);
        m.put("code",0);
        m.put("msg","");
        m.put("count",info.getTotal());
        System.err.println(info.getList());
        m.put("data",info.getList());
        return m;
    }
    @RequestMapping("insert")
    @ResponseBody
    public Map insert(Guru g){
        System.err.println(g);
        Map m = new HashMap();
        try {
            guruService.insert(g);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @RequestMapping("delete")
    @ResponseBody
    public Map delete(Integer id,Integer status){
        System.err.println(id+status);
        Map m = new HashMap();
        try {
//            guruService.delete(id,status);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @RequestMapping("addImg")
    @ResponseBody
    public Map addImg(MultipartFile file){
        Map map = new HashMap();
        try{
            file.transferTo(new File("D:\\img",file.getOriginalFilename()));
            map.put("flag",file.getOriginalFilename());
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("piliInsert")
    public void piliInsert(MultipartFile file) throws IOException {
        //读取excel文件
        FileInputStream inputStream = new FileInputStream(new File("D:\\img\\"+file.getOriginalFilename()));
        //读取文件的内容
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        //从文件中获取表对象 通过下表获取表对象
        HSSFSheet sheetAt = workbook.getSheetAt(0);
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            //通过下标获取行数据
            HSSFRow row = sheetAt.getRow(i);
            //从行中获取对应的列
            HSSFCell cell = row.getCell(0);
            double id = cell.getNumericCellValue();
            String image = row.getCell(1).getStringCellValue();
            String name = row.getCell(2).getStringCellValue();
            String nickName = row.getCell(3).getStringCellValue();
            double status = row.getCell(4).getNumericCellValue();
            System.err.println(id);
            System.err.println(image);
            System.err.println(name);
            System.err.println(nickName);
            System.err.println(status);
            Guru g = new Guru();
            g.setId((int) id);
            g.setImage(image);
            g.setName(name);
            g.setNickName(nickName);
            g.setStatus((int) status);
            guruService.insert(g);
        }
    }
    @RequestMapping("select")
    @ResponseBody
    public Map select(HttpServletResponse response,int id) throws IllegalAccessException, IOException {
        Map map = new HashMap();
        try {
            //1.查询所有数据
            PageInfo<Guru> info = guruService.selectAll(null, 1, 10);
            //2.将所有数据读入到workbook中
            HSSFWorkbook workbook = new HSSFWorkbook();
            String[] titles = {"上师编号", "上师头像地址", "上师姓名", "上师法号", "上师状态"};
            HSSFSheet sheet = workbook.createSheet("上师数据");
            HSSFRow row = sheet.createRow(0);
            for (int i = 0; i < titles.length; i++) {
                row.createCell(i).setCellValue(titles[i]);
            }
            //3.将上师数据放入对应的行中
            List<Guru> list = info.getList();
            for (int a = 0; a < list.size(); a++) {
                //创建行对象
                HSSFRow row1 = sheet.createRow(a + 1);
                //将集合中的上师对象遍历存放到Guru对象中
                Guru guru = list.get(a);
                //获取上师类对象
                Class<? extends Guru> guruClass = guru.getClass();
                //从类对象中获取上师的属性对象
                Field[] fields = guruClass.getDeclaredFields();
                //便利上师属性
                for (int i = 0; i < fields.length; i++) {
                    //创建单元格
                    HSSFCell cell = row1.createCell(i);
                    //拿到对象中的属性值
                    Field field = fields[i];
                    //设置允许通过反射访问该字段
                    field.setAccessible(true);
                    //获取该对象的成员变量的值
                    Object o = field.get(guru);
                    if(o!=null){
                        cell.setCellValue(o.toString());
                    }
                }
            }
//            File file = new File("D:\\img\\上师所有数据.xls");
//            FileOutputStream outputStream = new FileOutputStream(file);
            response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("上师", "UTF-8"));
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            workbook.write(response.getOutputStream());
//            FileUtils.copyFile(file,response.getOutputStream());
            map.put("flag",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",false);
        }
            return map;
    }
    //批量添加


}
